package com.aaa.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ActivityMqConfig {

    // 主题模式交换机：交换机，路由key

    public static final String ACTIVITY_EX = "activity-ex";// 交换机
    public static final String ACTIVITY_START_QUEUE = "activity-start-queue";// 活动队列:添加秒杀活动之后，保存到队列中
    public static final String ACTIVITY_KEY_START = "activity.start";

    public static final String ACTIVITY_END_QUEUE = "activity-end-queue";// 活动队列:添加秒杀活动之后，保存到队列中
    public static final String ACTIVITY_KEY_END = "activity.end";

    // 交换机
    @Bean(value = "activityEx")
    public TopicExchange activityEx() {
        return new TopicExchange(ACTIVITY_EX, true, false);
    }

    // 活动开始队列
    @Bean("activityStartQueue")
    public Queue activityStartQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 20);
        map.put("x-overflow", "drop-head");
        // 死信交换机
        map.put("x-dead-letter-exchange", com.aaa.mq.ActivityDeadMqConfig.ACTIVITY_DEAD_EX);
        // 路由key
        map.put("x-dead-letter-routing-key", "dead.start");
        return new Queue(ACTIVITY_START_QUEUE, true, true, false, map);
    }

    // 绑定
    @Bean(value = "bindActivityStartQueue")
    public Binding bindActivityStartQueue(@Qualifier("activityEx") TopicExchange exchange, @Qualifier("activityStartQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ACTIVITY_KEY_START);
    }

    // 活动结束队列
    @Bean("activityEndQueue")
    public Queue activityEndQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 20);
        map.put("x-overflow", "drop-head");
        // 死信交换机
        map.put("x-dead-letter-exchange", com.aaa.mq.ActivityDeadMqConfig.ACTIVITY_DEAD_EX);
        // 路由key
        map.put("x-dead-letter-routing-key", "dead.end");
        return new Queue(ACTIVITY_END_QUEUE, true, true, false, map);
    }

    // 绑定
    @Bean(value = "bindActivityEndQueue")
    public Binding bindActivityEndQueue(@Qualifier("activityEx") TopicExchange exchange, @Qualifier("activityEndQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ACTIVITY_KEY_END);
    }

}
